<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="rolename" id="rolename" lay-verify="required" lay-reqtext="角色名称不能为空" placeholder="角色名称" value="" class="layui-input" disabled>
        </div>
        <label class="layui-form-label required">角色功能：</label>
        <div class="layui-input-block">
            <div id="role" class="role-tree"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-radius layui-btn-primary" lay-submit lay-filter="saveBtn" id="saveBtn">确认保存</button>
            <button class="layui-btn layui-btn-radius layui-btn-primary"  id="backBtn">返回</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js" charset="utf-8"></script>
<script src="../../js/custorm/init.js"></script>
<script>
    layui.use(['form','treeSelect','tree', 'util'], function () {
        var form = layui.form,
            layer = layui.layer,tree=layui.tree,util = layui.util,
            $ = layui.$;//模拟数据
        var role={Id:0,Name:"",MenuId:[]}
        HavePermission('role-update')==true?$("#saveBtn").show():$("#saveBtn").hide();

        $(document).ready(function(){
            var content = window.location.href;
            var id = content.split('=')[content.split('=').length - 1];
            role.Id=parseInt(id) ;
            AxajGet('../../api/sys/GetRoleByRoleId?id='+id,'../../login.html',treeRender)
        });
          //开启复选框
        function treeRender(data){
            $('#rolename').val(data.role.name); 
            tree.render({
            elem: '#role'
            ,data: data.Menu
            ,showCheckbox: true,
            id:"role"
        });
        }
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            
            var checkedData = tree.getChecked('role'); //获取选中节点的数据
            var nodeIds = new Array();
            nodeIds = getCheckedId(checkedData);
            
            role.MenuId=nodeIds;
            role.Name=data.field.rolename;
            // if(nodeIds.length==0){
            //     layer.msg("父类型不能为空！");
            //     return ;
            // }
            AxajPost('../../api/sys/UpdateRole',role,'保存成功','../../login.html');
        });
        function getCheckedId(data) {
            var id = "";
            $.each(data, function (index, item) {
                if (id != "") {
                    id = id + "," + item.id;
                }
                else {
                    id =id+ item.id;
                }
                //item 没有children属性
                if (item.children != null) {
                    var i = getCheckedId(item.children);
                    if (i != "") {
                        id = id + "," + i;
                    }
                }
            });
            return id;
        }
        $("#backBtn").click(function () {
            history.back(-1);
        });
    });
</script>
</body>
</html>